//
//  XGMM2ViewController.m
//  FruitsWholesaler
//
//  Created by sgone-2 on 15/12/3.
//  Copyright © 2015年 sgone. All rights reserved.
//

#import "XGMM2ViewController.h"
#import "AFNetworking.h"
#import "Common.h"
#import "MBProgressHUD+MJ.h"

@interface XGMM2ViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;



@property(nonatomic,strong) UITextField *textMM1;
@property(nonatomic,strong) UITextField *textMM2;
@property(nonatomic,strong) UITextField *textMM3;

//网路请求
@property(nonatomic,strong)AFHTTPSessionManager *manager;


@end

@implementation XGMM2ViewController



//afnetworking网络请求数据
- (AFHTTPSessionManager *)manager
{
    if (_manager == nil) {
        _manager  = [AFHTTPSessionManager manager];
    }
    return _manager;
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLabel.text = @"设置密码";
    self.backBtn.hidden = NO;
    [self.rightBut setTitle:@"确定" forState:UIControlStateNormal];
    self.rightBut.hidden = NO;
    self.rightBut.titleLabel.font = [UIFont systemFontOfSize:17];

//    self.flag = @"0";
    
    //判断账户是否修改过
    
    [self panduan];
}




-(void)rightButClick:(id)sender
{

    
    
    
    [self.textMM1 resignFirstResponder];
    [self.textMM2 resignFirstResponder];
    [self.textMM3 resignFirstResponder];
    
    
    
    if ([self.flag isEqualToString:@"1"]) {
  
    
    if (self.textMM1.text.length == 0)
    {
        
        [self.view makeToast:@"当前密码不能为空"];
        return;
        
    }
    
    else if (self.textMM2.text.length == 0)
    {
        
        [self.view makeToast:@"新密码不能为空"];
        return;
        
    }
    else if (self.textMM3.text.length == 0)
    {
        [self.view makeToast:@"请再次输入新密码"];
        return;
        
    }
    
    
    else if (![self.textMM2.text isEqualToString:self.textMM3.text])
    {
        
        [self.view makeToast:@"输入新密码前后不一致"];
        return;
        
    }
    
        //MD5加密操作
        NSString *newPassword = md5(self.textMM2.text);
        NSString *oldPassword = md5(self.textMM1.text);


        
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        //输入密码进行网络修改
        NSString *urlStr = [NSString stringWithFormat:@"%@modify_apply_passwd",SERVER_ADDRESS2];
        NSDictionary *params = @{@"wTel" : self.dataHandler.phone , @"uToken" : self.dataHandler.uToken , @"apply_passwd" : newPassword , @"old_apply_passwd" : oldPassword};
        
        [self.manager POST:urlStr parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
            
            if ([responseObject[@"error_flag"] intValue] == 0) {
                
                
                [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
                UIAlertView *alter  = [[UIAlertView alloc]initWithTitle:@"提示" message:@"修改提现密码成功" delegate:nil cancelButtonTitle:nil
            otherButtonTitles:@"确定", nil];
                
                [alter show];
                
                [self.navigationController popViewControllerAnimated:YES];
                
                
                
                
            }
            else
            
            {
                
                
                [MBProgressHUD hideAllHUDsForView:self.view animated:YES];

                NSString *str1 = [NSString stringWithFormat:@"%@",responseObject[@"result_msg"]];
            
                UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:str1  delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                [alter show];
                
            
            }
            
            
           
            
            
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            
            
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];

            UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"上传失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定 ", nil];
            
            [alter show];
            
            
        }];

        
    }
    
    
    /*
    else
    {
    
    
        
        if (![self.textMM1.text isEqualToString:self.textMM2.text]) {
            
            
            [self.view makeToast:@"前后密码不一致"];
            
            return;
        }
        if (self.textMM1.text.length == 0) {
            
            
            [self.view makeToast:@"密码不能为空"];
            return;
        }
        if (self.textMM2.text.length == 0) {
            
            [self.view makeToast:@"重新输入密码不能为空"];
            return;
        }
        
        
        
        //MD5加密操作
        NSString *newPassword = md5(self.textMM1.text);
//        NSString *oldPassword = md5(self.textMM1.text);

        
        
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        
        [self.navigationController popViewControllerAnimated:YES];
   
        
//        NSString *urlString = [NSString stringWithFormat:@"%@modify_apply_passwd",SERVER_ADDRESS2];
//        NSDictionary*params = @{@"wTel" : self.dataHandler.phone ,@"uToken" : self.dataHandler.uToken , @"apply_passwd" : newPassword , @"old_apply_passwd" : @""};
//        [self.manager POST:urlString parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
//            if ([responseObject[@"error_flag"] intValue] == 0) {
//                
//                
//                
//                [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
//                UIAlertView *alter =[ [UIAlertView alloc]initWithTitle:@"提示" message:@"设置提现密码成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
//                
//                [alter show];
//                
//                
//                [self.navigationController popViewControllerAnimated:YES];
//                
//            }
//            
//            else
//            {
//                
//                  [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
//                NSString *str1 = [NSString stringWithFormat:@"%@",responseObject[@"result_msg"]];
//                
//                UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:str1  delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
//                [alter show];
//                
//            }
//            
//            
//            
//        } failure:^(NSURLSessionDataTask *task, NSError *error) {
//            
//            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
//            UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"上传失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定 ", nil];
//            
//            [alter show];
//            
//            
//            
//            
//        }];
//        
        
        
        
    
    
    
    
    }
*/
}

-(void) panduan
{


    
    if ([self.flag  isEqualToString:@"1"]) {
        //已经修改过展示重新修改密码界面
        self.title = @"修改提现密码";
        
        
        UILabel *lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 64, ViewWidth, 44)];
        lbl1.text = @"  当前密码";
        [self.view addSubview:lbl1];
        lbl1.font = [UIFont systemFontOfSize:15];
        lbl1.textColor = [UIColor grayColor];
        
        
        UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 64+44, 10, 50)];
        view2.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:view2];
        
        UIView *view23 = [[UIView alloc]initWithFrame:CGRectMake(ViewWidth-15, 64+44, 15, 50)];
        view23.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:view23];
        
        
        
        self.textMM1 = [[UITextField alloc]initWithFrame:CGRectMake(10, 64+44, ViewWidth-15, 50)];
        self.textMM1.placeholder = @"请输入当前密码";
        [self.view addSubview:self.textMM1];
        self.textMM1.backgroundColor = [UIColor whiteColor];
        self.textMM1.font = [UIFont systemFontOfSize:16];
        self.textMM1.secureTextEntry = YES;
        self.textMM1.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.textMM1.delegate = self;
        self.textMM1.returnKeyType = UIReturnKeyDone;
        
        
        
        
        
        
        UILabel *lbl2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 64+44 +50, ViewWidth, 44)];
        lbl2.text = @"  新密码";
        [self.view addSubview:lbl2];
        lbl2.font = [UIFont systemFontOfSize:15];
        lbl2.textColor = [UIColor grayColor];
        
        
        UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(0, 64+44*2+50, 10, 50)];
        view3.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:view3];
        
        UIView *view22 = [[UIView alloc]initWithFrame:CGRectMake(ViewWidth-15, 64+44*2+50, 15, 50)];
        view22.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:view22];
        
        
        
        
        self.textMM2 = [[UITextField alloc]initWithFrame:CGRectMake(10, 64+44*2+50, ViewWidth-15, 50)];
        self.textMM2.placeholder = @"请输入新密码";
        [self.view addSubview:self.textMM2];
        self.textMM2.backgroundColor = [UIColor whiteColor];
        self.textMM2.font = [UIFont systemFontOfSize:16];
        self.textMM2.secureTextEntry = YES;
        self.textMM2.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.textMM2.delegate = self;
        self.textMM2.returnKeyType = UIReturnKeyDone;
        
        
        
        UIView *view4 = [[UIView alloc]initWithFrame:CGRectMake(0, 64+44*2+50+50, 10, 50)];
        view4.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:view4];
        
        UIView *view21 = [[UIView alloc]initWithFrame:CGRectMake(ViewWidth-15, 64+44*2+50+50, 15, 50)];
        view21.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:view21];
        
        
        
        
        self.textMM3 = [[UITextField alloc]initWithFrame:CGRectMake(10, 64+44*2+50+50, ViewWidth-15, 50)];
        self.textMM3.placeholder = @"请再次输入新密码";
        [self.view addSubview:self.textMM3];
        self.textMM3.backgroundColor = [UIColor whiteColor];
        self.textMM3.font = [UIFont systemFontOfSize:16];
        self.textMM3.secureTextEntry = YES;
        self.textMM3.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.textMM3.delegate = self;
        self.textMM3.returnKeyType = UIReturnKeyDone;
        
        
        
        
        //中间的线
        UIView *view10 = [[UIView alloc]initWithFrame:CGRectMake(10, 64+44*2+50+50, ViewWidth-10, 0.8)];
        view10.backgroundColor = BACKGROUND1;
        [self.view addSubview:view10];
        
        
        

        
        
        
        
    }

    else
        
    {
    
    //没有修改进行操作
        self.title = @"设置提现密码";
    
        
        UILabel *lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 64, ViewWidth, 44)];
        lbl1.text = @"  请输入密码";
        [self.view addSubview:lbl1];
        lbl1.font = [UIFont systemFontOfSize:15];
        lbl1.textColor = [UIColor grayColor];
        
        
        UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 64+44, 10, 50)];
        view2.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:view2];
        
        UIView *view23 = [[UIView alloc]initWithFrame:CGRectMake(ViewWidth-15, 64+44, 15, 50)];
        view23.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:view23];
        
        
        
        self.textMM1 = [[UITextField alloc]initWithFrame:CGRectMake(10, 64+44, ViewWidth-15, 50)];
        self.textMM1.placeholder = @"请输入密码";
        [self.view addSubview:self.textMM1];
        self.textMM1.backgroundColor = [UIColor whiteColor];
        self.textMM1.font = [UIFont systemFontOfSize:16];
        self.textMM1.secureTextEntry = YES;
        self.textMM1.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.textMM1.delegate = self;
        self.textMM1.returnKeyType = UIReturnKeyDone;
        
        
        
        
        
        
        UILabel *lbl2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 64+44 +50, ViewWidth, 44)];
        lbl2.text = @"  请重新输入密码";
        [self.view addSubview:lbl2];
        lbl2.font = [UIFont systemFontOfSize:15];
        lbl2.textColor = [UIColor grayColor];
        
        
        UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(0, 64+44*2+50, 10, 50)];
        view3.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:view3];
        
        UIView *view22 = [[UIView alloc]initWithFrame:CGRectMake(ViewWidth-15, 64+44*2+50, 15, 50)];
        view22.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:view22];
        
        
        
        
        self.textMM2 = [[UITextField alloc]initWithFrame:CGRectMake(10, 64+44*2+50, ViewWidth-15, 50)];
        self.textMM2.placeholder = @"请重新输入密码";
        [self.view addSubview:self.textMM2];
        self.textMM2.backgroundColor = [UIColor whiteColor];
        self.textMM2.font = [UIFont systemFontOfSize:16];
        self.textMM2.secureTextEntry = YES;
        self.textMM2.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.textMM2.delegate = self;
        self.textMM2.returnKeyType = UIReturnKeyDone;
        
   
    
    
    }
    
    

}



-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    
    
    if (ViewHeight == 480.0) {
        
        
        if (textField == self.textMM3) {
            
            
            
            [UIView animateWithDuration:0.25 animations:^{
                if (self.view.y == 0) {
                    self.view.y = -100;
                }
            }completion:^(BOOL finished){
                //生成pickView
                
                
                
            }];
            
        }
    }
}


#pragma mark 收回键盘操作
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [textField resignFirstResponder];
    
    
    [UIView animateWithDuration:0.25 animations:^{
        if (self.view.y == -100) {
            self.view.y = 0;
        }
    }completion:^(BOOL finished){
        //生成pickView
        
        
        
    }];
    
    return YES;
    
    
    
}


@end
